using System;
using UnityEngine;

namespace ProceduralPrimitivesUtil
{
	public class ChamferCylinder : PPBase
	{
		[Header("Basic parameters")]
		public float radius = 0.5f;

		public float height = 1f;

		public float fillet = 0.1f;

		[Header("Segments")]
		public int sides = 20;

		public int capSegs = 2;

		public int heightSegs = 2;

		public int filletSegs = 3;

		[Header("Slice")]
		public bool sliceOn;

		public float sliceFrom;

		public float sliceTo = 360f;

		[Header("Mapping Coordinates")]
		public bool generateMappingCoords = true;

		public bool realWorldMapSize;

		public Vector2 UVOffset = new Vector2(0f, 0f);

		public Vector2 UVTiling = new Vector2(1f, 1f);

		[Header("Others")]
		public bool flipNormals;

		public bool smooth = true;

		protected override void CreateMesh()
		{
			m_mesh.name = "ChamferCylinder";
			radius = Mathf.Clamp(radius, 1E-05f, 10000f);
			height = Mathf.Clamp(height, 1E-05f, 10000f);
			float max = ((radius < height / 2f) ? radius : (height / 2f));
			fillet = Mathf.Clamp(fillet, 1E-05f, max);
			sides = Mathf.Clamp(sides, 3, 100);
			capSegs = Mathf.Clamp(capSegs, 1, 100);
			heightSegs = Mathf.Clamp(heightSegs, 1, 100);
			filletSegs = Mathf.Clamp(filletSegs, 1, 100);
			sliceFrom = Mathf.Clamp(sliceFrom, 0f, 360f);
			sliceTo = Mathf.Clamp(sliceTo, sliceFrom, 360f);
			float num = height * 0.5f;
			float num2 = num - fillet;
			float num3 = radius - fillet;
			Vector2 vector = (realWorldMapSize ? new Vector2(1f, 1f) : new Vector2(1f, 2f));
			vector = new Vector2(UVTiling.x * vector.x, UVTiling.y * vector.y);
			CreateCylinder(pivotOffset + Vector3.zero, m_rotation * Vector3.forward, m_rotation * Vector3.right, num2 * 2f, radius, sides, heightSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth);
			CreateCircle(pivotOffset + m_rotation * new Vector3(0f, num, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, num3, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals);
			CreateCircle(pivotOffset + m_rotation * new Vector3(0f, 0f - num, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, num3, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals);
			CreateTorus(pivotOffset + m_rotation * new Vector3(0f, 0f - num2, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, num3, fillet, sides, filletSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0f, 0f), vector, flipNormals, smooth, 90f, 180f);
			CreateTorus(pivotOffset + m_rotation * new Vector3(0f, num2, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, num3, fillet, sides, filletSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0f, 0.5f), vector, flipNormals, smooth, 180f, 270f);
			if (sliceOn)
			{
				float num4 = fillet * 0.5f;
				float num5 = num - num4;
				float num6 = radius - num4;
				float num7 = num3 * 0.5f;
				Vector3 vector2 = new Vector3(Mathf.Sin(sliceFrom * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceFrom * (MathF.PI / 180f)));
				Vector3 vector3 = new Vector3(Mathf.Sin(sliceTo * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceTo * (MathF.PI / 180f)));
				Vector2 vector4 = (realWorldMapSize ? new Vector2(1f, 1f) : new Vector2(fillet / radius, fillet / num));
				vector4 = new Vector2(UVTiling.x * vector4.x, UVTiling.y * vector4.y);
				Vector2 vector5 = (realWorldMapSize ? new Vector2(1f, 1f) : new Vector2(num7 / radius, num2 / num));
				vector5 = new Vector2(UVTiling.x * vector5.x, UVTiling.y * vector5.y);
				Vector2 vector6 = (realWorldMapSize ? new Vector2(1f, 1f) : new Vector2(num4 / radius, num2 / num));
				vector6 = new Vector2(UVTiling.x * vector6.x, UVTiling.y * vector6.y);
				Vector2 vector7 = (realWorldMapSize ? new Vector2(1f, 1f) : new Vector2(num7 / radius, fillet / height));
				vector7 = new Vector2(UVTiling.x * vector7.x, UVTiling.y * vector7.y);
				CreatePlane(pivotOffset + m_rotation * vector2 * num7, m_rotation * Vector3.up, m_rotation * -vector2.normalized, num3, num2 * 2f, capSegs, heightSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(num4 / radius, num4 / num), vector5, flipNormals);
				CreatePlane(pivotOffset + m_rotation * vector3 * num7, m_rotation * Vector3.up, m_rotation * vector3.normalized, num3, num2 * 2f, capSegs, heightSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0.5f, num4 / num), vector5, flipNormals);
				CreatePlane(pivotOffset + m_rotation * vector2 * num6, m_rotation * Vector3.up, m_rotation * -vector2.normalized, fillet, num2 * 2f, filletSegs, heightSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0f, num4 / num), vector6, flipNormals);
				CreatePlane(pivotOffset + m_rotation * vector3 * num6, m_rotation * Vector3.up, m_rotation * vector3.normalized, fillet, num2 * 2f, filletSegs, heightSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(num6 / radius, num4 / num), vector6, flipNormals);
				CreatePlane(pivotOffset + m_rotation * new Vector3(vector2.x * num7, num5, vector2.z * num7), m_rotation * Vector3.up, m_rotation * -vector2.normalized, num3, fillet, capSegs, filletSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(num4 / radius, (height - fillet) / height), vector7, flipNormals);
				CreatePlane(pivotOffset + m_rotation * new Vector3(vector2.x * num7, 0f - num5, vector2.z * num7), m_rotation * Vector3.up, m_rotation * -vector2.normalized, num3, fillet, capSegs, filletSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(num4 / radius, 0f), vector7, flipNormals);
				CreatePlane(pivotOffset + m_rotation * new Vector3(vector3.x * num7, num5, vector3.z * num7), m_rotation * Vector3.up, m_rotation * vector3.normalized, num3, fillet, capSegs, filletSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0.5f, (height - fillet) / height), vector7, flipNormals);
				CreatePlane(pivotOffset + m_rotation * new Vector3(vector3.x * num7, 0f - num5, vector3.z * num7), m_rotation * Vector3.up, m_rotation * vector3.normalized, num3, fillet, capSegs, filletSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0.5f, 0f), vector7, flipNormals);
				CreateCircle(pivotOffset + m_rotation * new Vector3(vector2.x * num3, num2, vector2.z * num3), m_rotation * Vector3.up, m_rotation * -vector2.normalized, fillet, filletSegs, filletSegs, sliceOn: true, 270f, 360f, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2((0f - num3) / (radius * 2f), num2 / height), vector4, flipNormals);
				CreateCircle(pivotOffset + m_rotation * new Vector3(vector2.x * num3, 0f - num2, vector2.z * num3), m_rotation * Vector3.up, m_rotation * -vector2.normalized, fillet, filletSegs, filletSegs, sliceOn: true, 180f, 270f, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2((0f - num3) / (radius * 2f), (0f - num2) / height), vector4, flipNormals);
				CreateCircle(pivotOffset + m_rotation * new Vector3(vector3.x * num3, num2, vector3.z * num3), m_rotation * Vector3.up, m_rotation * vector3.normalized, fillet, filletSegs, filletSegs, sliceOn: true, 0f, 90f, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(num3 / (radius * 2f), num2 / height), vector4, flipNormals);
				CreateCircle(pivotOffset + m_rotation * new Vector3(vector3.x * num3, 0f - num2, vector3.z * num3), m_rotation * Vector3.up, m_rotation * vector3.normalized, fillet, filletSegs, filletSegs, sliceOn: true, 90f, 180f, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(num3 / (radius * 2f), (0f - num2) / height), vector4, flipNormals);
			}
		}
	}
}
